package org.harlov.testsys.views;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import org.harlov.testsys.db.diffs;
import org.harlov.testsys.db.quests;
import org.harlov.testsys.db.tests;
import org.harlov.testsys.db.themes;
import org.harlov.testsys.db.suppdata.test_struct;
import org.harlov.testsys.views.add_forms.add_test_form;

public class creators_form extends JFrame {

	/**
	 * 
	 */
	private gui_factory gFactory;
	
	private String login;
	private JFrame mFrame; 
	
	private static final long serialVersionUID = 1L;
	private int sel_test = 0;
	private int sel_quest = 0;
	
	public creators_form (String _login) {
		
		super("HTest|Режим Разработчика|"+_login);
		gFactory = new gui_factory(this);
		mFrame = this;
		login = _login;
		
		this.setPreferredSize(new Dimension(800,700));
		this.setSize(800,700);
		this.getContentPane().setLayout(null);
		this.setResizable(false);
		gui_factory.setCenterPosition(this);
		this.setVisible(true);
		this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0);} });
		
		//USER INTERFACE
		//***********************************************
		
		gFactory.newLabel("Список тестов",	"l_tests_list",		20,	10,	200,	20);
		gFactory.newLabel("Список вопросов","l_quests_list",	590,10,	200,	20);
		gFactory.newLabel("Текст вопроса", "l_qtext", 240,10,200,20);
		gFactory.newLabel("Неправильные ответы","l_fans", 240,270,200,20 );
		gFactory.newLabel("Правильные ответы", "l_tans", 240, 390,200,20);
		
		gFactory.newText("tx_themeadd", 170, 615, 100, 20);
		gFactory.newButton("Добавить тему", "btn_theme_add", 10, 620, 150,20,new addTheme());
		
		
		
		gFactory.newLabel("Количество вопросов", "l_quests_count", 230,520,200,20);
		gFactory.newLabel("Продолжительность", "l_time", 230,540,200,20);
		gFactory.newLabel("Название теста", "l_test_name", 440,560,120,20);
		gFactory.newList("tests_list",	tests.getTestsView(login) ,	20,	30,	200,	500,new test_list_click());
		gFactory.newList("quests_list",	quests.getQuestsView(0),	590,30,	200,	500,new quest_list_click());
		gFactory.newTextArea("quest_text",	230,	30,	340,	240);
		gFactory.newTextArea("fans_text", 230, 290, 340, 100);
		gFactory.newTextArea("tans_text", 230,	410, 340,100);
		gFactory.newSpinner("sp_quests_count", 470,520,100, 20);
		gFactory.newSpinner("sp_times", 470, 540, 100,20);
		
		
		gFactory.newButton("+Тест",			"btn_test_add",		20,		530,	100,	30,	new add_new_test());
		gFactory.newButton("-Тест",			"btn_test_del",		120,	530,	100,	30,	new del_test());
		gFactory.newButton("+Вопрос",		"btn_quest_add",	590,	530,	100,	30, new add_new_quest());
		gFactory.newButton("-Вопрос",		"btn_quest_del",	690,	530,	100,	30,	new del_quest());
		gFactory.newButton("Сохранить вопрос","btn_save_quest",	590,	560,	200,	40, new save_quest() );
		gFactory.newButton("Сохранить параметры", "btn_save_test",20,	560,	200,	40, new save_test());
		gFactory.newComboBox("cb_theme",themes.getThemes(), 230, 560);
		gFactory.newComboBox("cb_complexs", diffs.getDiffs(), 230, 580);
		gFactory.newText("tx_test_name", 440, 580,140,20);
		
		//**********************************************
		
	}
	
	
	class addTheme implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			themes.addTheme(gFactory.getTextFrom("tx_themeadd"));
		}
	}
	
	public void compRepaint(String name) {
		JComponent temp = gFactory.getObject(name);
		temp.repaint();
	}
	
	
	
	class add_new_test implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			new add_test_form(login,(creators_form)mFrame);			
		}
		
	}
	
	class del_test implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			ArrayList<test_struct> tests_list =	tests.getTests(login);
			test_struct temp = tests_list.get(gFactory.getSelList("tests_list")-1);
			tests.delTest(temp.getId());	
			tests.getTests(login);
			tests.getTestsView(login);
			gFactory.CompRepaint("tests_list");
		}
		
	}
	
	class save_test implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			tests.saveTest(sel_test, gFactory.getSelCombo("cb_theme")-1,gFactory.getSelCombo("cb_complexs"), gFactory.getTextFrom("tx_test_name"), gFactory.getValueSpinner("sp_times"),gFactory.getValueSpinner("sp_quests_count"));
			tests.getTestsView(login);
			gFactory.CompRepaint("tests_list");
		}
		
	}
	
	class add_new_quest implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			ArrayList<test_struct> tests_list =	tests.getTests(login);
			test_struct temp = tests_list.get(gFactory.getSelList("tests_list")-1);
			quests.addQuest(temp.getId());	
			gFactory.CompRepaint("quests_list");
		}
		 
	
	}
	
	class del_quest implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			quests.delQuest(gFactory.getSelList("tests_list")-1);
		}
		
	}
	
	class test_list_click implements ListSelectionListener {

		@Override
		public void valueChanged(ListSelectionEvent arg0) {
		gFactory.delSel("quests_list");
		try{
			ArrayList<test_struct> _tests_list = tests.getTests(login);
		if (gFactory.getSelList("tests_list") != 0) {
			test_struct temp = _tests_list.get(gFactory.getSelList("tests_list")-1);
			sel_test = temp.getId();
			gFactory.setValueSpinner("sp_times", temp.getTimeAv());
			gFactory.SetText(temp.getName(),"tx_test_name");
			gFactory.setSelCombo("cb_theme", themes.getListbyId(temp.getTheme()));
			gFactory.setSelCombo("cb_complexs", temp.getDiff()-1);
			quests.getQuestsView(sel_test);
			gFactory.CompRepaint("quests_list");
			gFactory.setValueSpinner("sp_quests_count",temp.getQuestsCount() );
		}
		}
		catch (Exception e) {e.printStackTrace();}
		}
		
	}
	
	class quest_list_click implements ListSelectionListener {

		@Override
		public void valueChanged(ListSelectionEvent e) {
			quests.getQuests(sel_test);
			int sel_quest = gFactory.getSelList("quests_list");
	
			String[] t_ans = quests.getQuestTAnsView(sel_quest);  //Получаем правильные ответ
			if (t_ans != null) {
			StringBuilder result = new StringBuilder();
			
			for (String temp_s:t_ans) {
				result.append(temp_s);
				result.append("\n");
			}
			gFactory.SetTextToArea(result.toString(), "tans_text");
			}
			else gFactory.SetTextToArea(null, "tans_text");
			
			
			
			
			
			String[] f_ans = quests.getQuestFAnsView(sel_quest); //Неправильные ответы
			if (f_ans != null) {
			StringBuilder result = new StringBuilder();
			
			for (String temp_s:f_ans) {
				result.append(temp_s);
				result.append("\n");
			}
			gFactory.SetTextToArea(result.toString(), "fans_text"); 
			}
			else gFactory.SetTextToArea(null, "fans_text");
			
			String q_text = quests.getQuestText(gFactory.getSelList("quests_list")); //Текст вопроса
			if (q_text!= null) {
				gFactory.SetTextToArea(q_text,"quest_text");
			}
			else gFactory.SetTextToArea(null,"quest_text");
			
		}
		
	}
	
	class save_quest implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
				JTextArea tans_text = (JTextArea) gFactory.getObject("tans_text");
				JTextArea fans_text = (JTextArea) gFactory.getObject("fans_text");
				JTextArea t_quest = (JTextArea) gFactory.getObject("quest_text");
				String q_text = t_quest.getText();
				String temp1 = tans_text.getText();
				String temp2 = fans_text.getText();
				String[] tans = temp1.split("\n");
				String[] fans = temp2.split("\n");
				
				quests.saveQuest(q_text,tans,fans);
				
				
				
		}
		
	}
	
	
}
